package com.hxkcloud.nurse.activitys;


import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.recyclebanner.InitRecycleViewpager;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.DpToPxUtils;
import com.hxkcloud.nurse.utils.ImageOptionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.activitys
 * Description:
 * Create by Ray.
 * Date: 2016/10/9 16:15
 */
public class DisplayImageActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_delete;

    private ImageView currentPicIV;
    private String currentPath = "";

    private String isHideDelete = "";

    private String title = "1/1";
    private InitRecycleViewpager initRecycleViewpager;
    private List<String> imageBeanList = new ArrayList<>();
    private int currentIndex = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_displaypic);
        if (getIntent().hasExtra("isHideDelete")) {
            isHideDelete = getIntent().getStringExtra("isHideDelete");
        }
        if (getIntent().hasExtra("title")) {
            title = getIntent().getStringExtra("title");
        }
        if (getIntent().hasExtra("position")) {
            currentIndex = getIntent().getIntExtra("position", 0);
        }
        if (getIntent().hasExtra("pics")) {
            imageBeanList = getIntent().getStringArrayListExtra("pics");
        } else {
            if (getIntent().hasExtra("path")) {
                imageBeanList.add(getIntent().getStringExtra("path"));
            }
        }

        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_delete = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(title);
        if ("true".equals(isHideDelete)) {
            tv_delete.setVisibility(View.INVISIBLE);
        } else {
            tv_delete.setVisibility(View.VISIBLE);
        }
        tv_delete.setText(getResources().getString(R.string.displaypic_text_delete));
        tv_delete.setOnClickListener(this);

        currentPicIV = (ImageView) findViewById(R.id.displaypic_iv_pic);
        initRecycleViewpager = new InitRecycleViewpager();
        initRecycleViewpager.initView(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
     /*   if (getIntent().hasExtra("path")) {
            currentPath = getIntent().getStringExtra("path");
            if (currentPath.startsWith("http")) {
                imageLoader.displayImage(currentPath, currentPicIV, ImageOptionUtils.getInstance().getSimplePicOptions());
            } else {
                File cameraFile = new File(currentPath);
                if (cameraFile != null) {
                    ImageVideoUtils.compressPicture(cameraFile, cameraFile);
                    Bitmap mBitmap = BitmapFactory.decodeFile(cameraFile.getAbsolutePath());
                    currentPicIV.setImageBitmap(mBitmap);
                }
            }

        }*/
        if (imageBeanList != null) {
            // initRecycleViewpager.getInfinite_Pager().setOnPageChangeListener(this);
            initRecycleViewpager.initData(R.layout.viewpager_item, imageBeanList, new InitRecycleViewpager.BannerItemResponse() {
                @Override
                public void responseData(View convertView, int position) {
                    ImageView imageView_ads_icon = (ImageView) convertView.findViewById(R.id.imageView_ads_icon);
                    currentPath = imageBeanList.get(position);
                    if (!currentPath.startsWith("http")) {
                        ImageLoader.getInstance().displayImage("file://" + currentPath,
                                imageView_ads_icon, ImageOptionUtils.getInstance().getRoundPicOptions(DpToPxUtils.dip2px(mContext, 4)));
                    } else {
                        imageLoader.displayImage(currentPath, imageView_ads_icon, ImageOptionUtils.getInstance().getSimplePicOptions());
                    }

                }
            });
            initRecycleViewpager.setCurrentPosition(currentIndex);
            tv_title.setText((currentIndex + 1) + "/" + imageBeanList.size());
            initRecycleViewpager.setPageChangeListener(new InitRecycleViewpager.onCustomPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    currentIndex = position;
                    tv_title.setText((currentIndex + 1) + "/" + imageBeanList.size());
                }
            });
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                Intent iBack = new Intent(mContext, MyReportInputActivity.class);
                iBack.putExtra("position", currentIndex);
                setResult(RESULT_OK, iBack);
                finish();
                break;
        }
    }
}
